
##class StyTextElem(object):
##    __slots__ = ()
##    def __init__(self, text, 

class StyledText(object):
    __slots__ = ('_len', '_elems')
    def __init__(self):
        self._len = 0
        self._elems = []
    def __len__(self):
        return self._len
    def __iter__(self):
        raise NotImplementedError("Can't iterate over StyledText characters")
    def __str__(self):
        return ''.join([el[0] for el in self._elems])
    def __getitem__(self, idx):
        if idx < 0:
            idx += self._len
        if idx < 0 or idx >= self._len:
            raise IndexError('%s index out of range' % self.__class__.__name__)
        for elem in self._elems:
            cnt = len(elem[0])
            if idx < cnt:
                return (elem[0][idx],) + elem[1:]
            idx -= cnt
    def __getslice__(self, idx, jdx):
        res = self.__class__()
        for elem in self._elems:
            if jdx <= 0:
                break
            cnt = len(elem[0])
            if idx <= 0 and jdx >= cnt:
                res.append(elem)
            elif 0 <= idx < cnt:
                res.append((elem[0][idx:jdx],) + elem[1:])
            elif jdx < cnt:
                res.append((elem[0][:jdx],) + elem[1:])
            idx -= cnt
            jdx -= cnt
        return res
    def __iadd__(self, other):
        pass
    def append(self, elem):
        if elem[0]:
            self._len += len(elem[0])
            last = self._elems[-1] if self._elems else None
            if last and last[1:] == elem[1:]:
                self._elems[-1] = (last[0] + elem[0],) + last[1:]
            else:
                self._elems.append(elem)
    def iterelems(self):
        return iter(self._elems)

class BufferElement(object):
    __slots__ = ('text',)

class RingBuffer(object):
    def __init__(self, size):
        self._buffer = []
        self._size = size
        self._head = -1
    def __len__(self):
        return len(self._buffer)
    def __getitem__(self, idx):
        if idx < 0:
            idx += len(self._buffer)
        if idx < 0 or idx >= len(self._buffer):
            raise IndexError('%s index out of range' % self.__class__.__name__)
        idx = (self._head - idx) % self._size
        return self._buffer[idx]
    def __getslice__(self, idx, jdx):
        idx = (self._head - idx) % self._size
        jdx = (self._head - jdx) % self._size
        print idx, jdx
        if idx >= jdx:
            return self._buffer[idx:jdx:-1]
        else:
            return self._buffer[idx::-1] + self._buffer[:jdx:-1]
    def push(self, data):
        self._head = (self._head + 1) % self._size
        if self._head >= len(self._buffer):
            self._buffer.append(data)
        else:
            self._buffer[self._head] = data

class AnsiParser(object):
    def parse(self, data):
        
##class PageBuffer(object):
##    def __init__(self, size):
##        self._buffer = [BufferElement()]
##        self._head = 0
####        self._tail = 0
####    def isempty(self):
####        return self._head == self._tail
##    def add(self, text):
##        elem = self._buffer[self._head]
##        elem.text += text
##    def tail(self):
##        return (self_head + 1) % self._length if len(self._buffer) == self._length else 0
##    def wintop(self):
##        self._winbase - self._winlen
##    def incr(self):
##        if len(self._buffer) < self._length:
##            self._head += 1
##            self._buffer.append(BufferElement())
##        else:
##            self._head = (self._head + 1) % self._length
##            self._buffer[self._head] = BufferElement()
####        if self._head == self._tail:
####            self._tail = (self._tail + 1) % self._length
##        if self._paused:
##            pass
##        else:
##            self._winbase = self._head
##            self._wintop = 
        
        
##class TerminalEmulator(wx.Window):
##    def __init__(self, parent, id, title):
##        wx.Window.__init__(self, parent, id, title)
##    def AddText(self, text):
##        pass
##    @property
##    def PageSize(self):
##        return self._pagesize
##    @PageSize.setter(self, size):
##        pass
    
